<template>
	<view class="ys">
		<view class="no-list" v-if="list.length == 0">
			<image src="/static/index/nolist.png" mode="widthFix"></image>
			- 暂无数据 -
		</view>
		<view class="list" v-else>
			<view class="item" v-for="(item,index) in list" :key="index"
				@click="permission == 'all'?toPage(`./add?id=${item.id}`):''">
				<view class="imgbox">
					<image :src="item.avatar || siteConf.logo" mode=""></image>
				</view>
				<view class="info">
					<view class="name">
						{{item.name}}
						<!-- <text :style="{background:item.reg_type.color}">{{item.reg_type.text}}</text> -->
					</view>
					<view class="name">
						<text v-for="(l,l_index) in item.label" :key="l_index"
							:style="{background:l.color}">{{l.text}}</text>
					</view>
					<view class="phone">
						{{item.phone || '未填'}}
					</view>
					<view class="time">
						{{item.create_time}}
					</view>
				</view>
				<view class="btns" v-if="permission == 'all'">
					<button :style="{background:item.show_btn.color}" v-if="item.show_btn"
						@click="setStaffStatus(item)">{{item.show_btn.text}}</button>
					<button :style="{background:'#8651ed'}" v-if="item.show_verify"
						@click.stop="verifyStaff(item,index)">审核通过</button>
					<button :style="{background:'red'}" v-if="item.show_verify"
						@click.stop="delStaff(item,index)">删除</button>
				</view>
			</view>
		</view>
		<view class="add qr_img" @click="createQr" v-if="permission == 'all'">
			<image src="/static/image/qr.svg" mode=""></image>
		</view>
		<view class="add" @click="toPage('./add')" v-if="permission == 'all'">
			<image src="/static/index/add2.png" mode=""></image>
		</view>
	</view>
</template>

<script>
	import {
		getStaff,
		setStaffStatus,
		createQr,
		verifyStaff,
		delStaff
	} from "@/api/user.js"
	export default {
		data() {
			return {
				permission: this.$store.state.permission,
				list: []
			}
		},
		onShow() {
			console.log(this.$store);
			this.getStaff()
		},
		methods: {
			//生成二维码
			createQr() {
				createQr().then(res => {
					if (res.code == 0) {
						uni.previewImage({
							urls: [res.data.qrcode]
						})
					} else {
						uni.$showMsg(res.msg)
					}
				})
			},
			verifyStaff(item, index) {
				let that = this
				uni.showModal({
					title: '审核员工',
					content: "确定要审核通过吗",
					success(data) {
						if (data.confirm) {
							verifyStaff({
								staff_id: item.id
							}).then(res => {
								uni.$showMsg(res.msg)
								if (res.code == 0) {
									that.$set(that.list, index, {
										...item,
										...res.data
									})
								}
							})
						}

					}
				})
			},
			delStaff(item, index) {
				let that = this
				uni.showModal({
					title: '审核员工',
					content: "确定要删除此员工吗",
					success(data) {
						if (data.confirm) {
							delStaff({
								staff_id: item.id
							}).then(res => {
								uni.$showMsg(res.msg)
								if (res.code == 0) {
									that.list.splice(index, 1)
								}
							})
						}

					}
				})
			},
			setStaffStatus(item) {
				var that = this
				uni.showModal({
					title: '提示',
					content: '确认此操作吗?',
					success(res) {
						if (res.confirm) {
							setStaffStatus({
								staff_id: item.id,
								status: item.show_btn.status
							}).then(res => {
								uni.$showMsg(res.msg)
								if (res.code == 0) {
									that.getStaff()
								}
							})
						}
					}
				})

			},
			getStaff() {
				getStaff().then(res => {
					if (res.code == 0) {
						this.list = res.data.list
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	.add {
		position: fixed;
		z-index: 99;
		left: 10px;
		bottom: 50px;

		image {
			width: 60px;
			height: 60px;
		}
	}

	.qr_img {
		bottom: 130px;
		background-color: #8651ed;
		border-radius: 50%;
		display: flex;
		justify-content: center;
		align-items: center;
		width: 60px;
		height: 60px;

		image {
			width: 40px;
			height: 40px;
		}
	}

	.list {
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 15px 10px;
		box-sizing: border-box;
		flex-wrap: wrap;

		.item {
			background: #fff;
			border-radius: 6px;
			padding: 10px;
			box-sizing: border-box;
			width: 100%;
			margin-bottom: 15px;
			box-shadow: 0 0 5px #eee;
			display: flex;
			align-items: center;
			justify-content: space-between;

			.imgbox {
				width: 60px;
				height: 60px;

				image {
					width: 100%;
					height: 100%;
				}
			}

			.btns {
				display: flex;
				justify-content: flex-end;
				flex-direction: column;
				width: 60px;

				button {
					font-size: 12px;
					margin: 0;
					padding: 0;
					width: 60px;
					background: #b60000;
					color: #fff;
					margin: 5px 0;
				}
			}

			.info {
				width: calc(100% - 120px);
				padding-left: 20px;
				box-sizing: border-box;

				.name {
					font-weight: bold;
					font-size: 14px;
					display: flex;
					align-items: center;
					flex-wrap: wrap;

					text {
						margin-right: 10px;
						background: $uni-bg-color;
						font-size: 12px;
						color: #fff;
						padding: 2px 8px;
						border-radius: 4px;
						margin-top: 4px;
					}
				}

				.phone {
					margin: 5px 0;
					font-size: 13px;
				}

				.time {
					color: #777;
					font-size: 12px;
				}
			}
		}
	}
</style>